package cn.u313.service;

import cn.u313.comm.conststring.TxtzhuFU;
import cn.u313.comm.conststring.粽子;
import cn.u313.comm.eunm.BlessingConst;
import cn.u313.comm.eunm.Type;
import cn.u313.comm.utils.ResultBlessing;
import cn.u313.comm.utils.Util;
import cn.u313.config.NewYearConfig;
import cn.u313.entry.BlessingEntry;
import cn.u313.entry.DragonBoatFestivalEntry;
import cn.u313.entry.JiLuEntry;
import cn.u313.entry.UserEntry;
import cn.u313.model.MsgInfo;
import cn.u313.repository.BlessingEntryRepository;
import cn.u313.repository.DragonBoatFestivalRepository;
import cn.u313.repository.JiLuEntryRepository;
import cn.u313.repository.UserEntryRepository;
import me.kuku.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import static cn.u313.comm.conststring.粽子.*;
import static cn.u313.comm.utils.Util.getTime;
import static cn.u313.comm.utils.Util.icon;
@Transactional
@Service
public class DragonBoatFestivalService {
    private int price = 20;
    private int max = 10;
    private int maxZongzi = 3;

    @Autowired
    DragonBoatFestivalRepository dragonBoatFestivalRepository;
    @Autowired
    UserEntryService userEntryService;
    @Autowired
    JiLuEntryRepository jiLuEntryRepository;
    @Autowired
    UserEntryRepository userEntryRepository;


    public String executeZongZi(String qq, String name, boolean is咸, String group) {
        /**
         * 判断今日是否满了
         */
        int count =  dragonBoatFestivalRepository.countByDateDow(qq,粽子.包粽子);
        System.out.println(count);
        if (maxZongzi<count)return "今日包粽子 3次 机会用完了哦！";

        DragonBoatFestivalEntry dragonBoatFestivalEntry = new DragonBoatFestivalEntry();
        dragonBoatFestivalEntry.setAddDate(Util.getTime());
        dragonBoatFestivalEntry.setId(MyUtils.randomNT(3));
        dragonBoatFestivalEntry.setQq(qq);
        dragonBoatFestivalEntry.setZong(粽子.toZongZi(name,is咸));
        dragonBoatFestivalEntry.setHasInvalid(0);
        dragonBoatFestivalEntry.setRefZong(粽子.包粽子);
        dragonBoatFestivalEntry.setUGroup(group);


        /**
         * 记录操作
         */
        JiLuEntry y = new JiLuEntry();

        y.setQ0n(0);
        y.setQ1n(0);
        y.setQq0(qq);
        y.setTime(getTime());
        y.setType(粽子.包粽子);
        y.setMsg(dragonBoatFestivalEntry.getZong());
        y.setGroupNumber(group);

        jiLuEntryRepository.save(y);
        dragonBoatFestivalRepository.save(dragonBoatFestivalEntry);

        return "成功获得一个粽子\n原来你也爱吃"+dragonBoatFestivalEntry.getZong()+"呀";
    }

    /**
     * 粽子列表
     * @param qq
     * @param group
     * @param isMe
     * @return
     */
    public String showZongZiList(String qq, String group,boolean isMe) {
        List<DragonBoatFestivalEntry> list = dragonBoatFestivalRepository.byQQAndToDay(qq);
        if (list == null) {
            return "暂时没有粽子哦！\n快来一起包粽子吧";
        }
        StringJoiner str0 = new StringJoiner("\n","","");
        for (DragonBoatFestivalEntry dragonBoatFestivalEntry : list) {
            str0.add("【"+dragonBoatFestivalEntry.getId()+"】 美味的 "+dragonBoatFestivalEntry.getZong());
        }
        String str = "";
        if (isMe){
            str ="您当前未失效的粽子如下：\n"+str0;
        }else
            str ="您当前未失效的粽子如下：\n"+str0+"\n回复：赠送粽子【编号】";
        return str;

    }

    public String zs(String qq, String qq1, String group, String code) {
        if (qq.equals(qq1))return "留着自己吃吧你！";
        DragonBoatFestivalEntry dragonBoatFestivalEntry = dragonBoatFestivalRepository.getById(qq,code);
        if (dragonBoatFestivalEntry == null) {
            return "这粽子 【"+code+"】怕不是你的吧";
        }



        int num1 = jiLuEntryRepository.countByqqAndType(qq,粽子.送粽子);
        if (num1>0&&!"2036085374".equals(qq1))return "一天只能赠送他人一回粽子哦！";

        int num2 = jiLuEntryRepository.countByqq1AndType(qq1,粽子.送粽子);
        if (num2>0&&!"2036085374".equals(qq1))return "对方一天只能收一回粽子哦！";

        int num3 = jiLuEntryRepository.countQqToQq1andType(qq,qq1,粽子.送粽子);
        if (num3>0)return "今天已经送过ta粽子啦！";


        dragonBoatFestivalEntry.setHasInvalid(1);

        DragonBoatFestivalEntry dragonBoatFestivalEntry0 = new DragonBoatFestivalEntry();
        dragonBoatFestivalEntry0.setAddDate(Util.getTime());
        dragonBoatFestivalEntry0.setId(MyUtils.randomNT(2));
        dragonBoatFestivalEntry0.setQq(qq1);
        dragonBoatFestivalEntry0.setZong(dragonBoatFestivalEntry.getZong());
        dragonBoatFestivalEntry0.setHasInvalid(0);
        dragonBoatFestivalEntry0.setRefZong(dragonBoatFestivalEntry.getQq());
        dragonBoatFestivalEntry0.setUGroup(group);

        /**
         * 记录操作
         */
        JiLuEntry y = new JiLuEntry();
        y.setQ0n(0);
        y.setQ1n(0);
        y.setQq0(qq);
        y.setCaozuoId(dragonBoatFestivalEntry.getId()+"|"+dragonBoatFestivalEntry0.getId());
        y.setQq1(qq1);
        y.setTime(getTime());
        y.setType(粽子.送粽子);
        y.setMsg(dragonBoatFestivalEntry.getZong());
        y.setGroupNumber(group);



        dragonBoatFestivalRepository.save(dragonBoatFestivalEntry);
        dragonBoatFestivalRepository.save(dragonBoatFestivalEntry0);
        jiLuEntryRepository.save(y);

        if("2036085374".equals(qq1)){

            int yb = 粽子.eatYb();
            yb =yb>0?yb:1;
            double hb = 粽子.eatHb();

            UserEntry userEntry = userEntryService.getUser(qq);
            userEntry.payMe(yb);
            userEntry.payMe(hb);
            userEntryRepository.save(userEntry);
            return "谢谢你的"+dragonBoatFestivalEntry.getZong()+"\n小嘤很喜欢哦！\n小嘤也送你一份小礼品吧\n悦币奖励："+yb+"\n红包奖励："+hb+"\n[CAT:image,url=http://gchat.qpic.cn/gchatpic_new/1054980699/2642511933-2477881023-4EB287017D1B17594D0C35D7960F4130/0?term&#61;3]\n";
        }
        return "成功送出一份端午祝福！";
    }

    public String eat(String qq, String group, String code) {
        /**
         * 粽子详情
         */

        DragonBoatFestivalEntry dragonBoatFestivalEntry = dragonBoatFestivalRepository.getById(qq,code);
        if (dragonBoatFestivalEntry == null) {
            return "这粽子 【"+code+"】怕不是你的吧";
        }

        int yb = 粽子.eatYb()-3;
        yb =yb>0?yb:1;
        double hb = 粽子.eatHb();
        dragonBoatFestivalEntry.setHasInvalid(1);
        dragonBoatFestivalEntry.setRewardRef("吃粽子");
        dragonBoatFestivalEntry.setRewardZ(yb+"|"+hb);

        UserEntry userEntry = userEntryService.getUser(qq);
        userEntry.payMe(yb);
        userEntry.payMe(hb);
        /**
         * 记录操作
         */
        JiLuEntry y = new JiLuEntry();
        y.setQ0n(0);
        y.setQ1n(0);
        y.setQq0(qq);
        y.setTime(getTime());
        y.setType(粽子.吃粽子);
        y.setCaozuoId(dragonBoatFestivalEntry.getId());
        y.setMsg(dragonBoatFestivalEntry.getZong());
        y.setGroupNumber(group);
        userEntryRepository.save(userEntry);
        jiLuEntryRepository.save(y);
        dragonBoatFestivalRepository.save(dragonBoatFestivalEntry);
        return "美味的粽子✨ 嘿嘿\n获得悦币："+yb+"\n获得红包："+hb;
    }

    public String robbery(String qq, String group, String qq1) {
        if (qq1 == null) {
            return "你需要抢谁的粽子呀?";
        }
        int v = (int) (Math.random()*1000)%100;
        /**
         * 检查今天次数
         */
        int num = jiLuEntryRepository.countByqq1AndType(qq,粽子.抢粽子);
        if (num>=3)return "今天已经抢了3次了哦\n明天再来啦！";

        // TODO: 2022/5/26 抢成功
        if (v<30){
            List<DragonBoatFestivalEntry> dragonBoatFestivalEntries = dragonBoatFestivalRepository.byQQAndToDay(qq1);
            if (dragonBoatFestivalEntries==null||dragonBoatFestivalEntries.size()==0){
                return "对方今天还没有粽子哦\n快给ta送一个吧";
            }
            if (dragonBoatFestivalEntries.size()==1){
                return "对方只有一个粽子了\n给ta留一个吧";
            }
            DragonBoatFestivalEntry dragonBoatFestivalEntry0 = dragonBoatFestivalEntries.get(0);
            dragonBoatFestivalEntry0.setHasInvalid(1);

            DragonBoatFestivalEntry dragonBoatFestivalEntry = new DragonBoatFestivalEntry();
            dragonBoatFestivalEntry.setAddDate(Util.getTime());
            dragonBoatFestivalEntry.setId(MyUtils.randomNT(2));
            dragonBoatFestivalEntry.setQq(qq);
            dragonBoatFestivalEntry.setZong(dragonBoatFestivalEntry0.getZong());
            dragonBoatFestivalEntry.setHasInvalid(0);
            dragonBoatFestivalEntry.setRefZong(dragonBoatFestivalEntry0.getQq());
            dragonBoatFestivalEntry.setUGroup(group);

            /**
             * 记录操作
             */
            JiLuEntry y = new JiLuEntry();
            y.setQ0n(0);
            y.setQ1n(-1);
            y.setQq0(qq);
            y.setQq1(qq1);
            y.setTime(getTime());
            y.setType(粽子.抢粽子);
            y.setCaozuoId(dragonBoatFestivalEntry.getId());
            y.setMsg(dragonBoatFestivalEntry.getZong());
            y.setGroupNumber(group);
            jiLuEntryRepository.save(y);

            dragonBoatFestivalRepository.save(dragonBoatFestivalEntry0);
            dragonBoatFestivalRepository.save(dragonBoatFestivalEntry);
            return "厉害了呦！\n成功抢到对方一个["+dragonBoatFestivalEntry.getZong()+"]";
        }
        else if (v<45){// TODO: 2022/5/26 抢失败 被抢
            List<DragonBoatFestivalEntry> dragonBoatFestivalEntries = dragonBoatFestivalRepository.byQQAndToDay(qq);
            if (dragonBoatFestivalEntries.size()<=1){
                return "不要气馁哦！\n说不定下次就成功了呢！";
            }
            DragonBoatFestivalEntry dragonBoatFestivalEntry0 = dragonBoatFestivalEntries.get(0);
            dragonBoatFestivalEntry0.setHasInvalid(1);

            DragonBoatFestivalEntry dragonBoatFestivalEntry = new DragonBoatFestivalEntry();
            dragonBoatFestivalEntry.setAddDate(Util.getTime());
            dragonBoatFestivalEntry.setId(MyUtils.randomNT(2));
            dragonBoatFestivalEntry.setQq(qq1);
            dragonBoatFestivalEntry.setZong(dragonBoatFestivalEntry0.getZong());
            dragonBoatFestivalEntry.setHasInvalid(0);
            dragonBoatFestivalEntry.setRefZong(dragonBoatFestivalEntry0.getQq());
            dragonBoatFestivalEntry.setUGroup(group);

            /**
             * 记录操作
             */
            JiLuEntry y = new JiLuEntry();
            y.setQ0n(-1);
            y.setQ1n(0);
            y.setQq0(qq);
            y.setQq1(qq1);
            y.setTime(getTime());
            y.setType(粽子.抢粽子);
            y.setCaozuoId(dragonBoatFestivalEntry.getId());
            y.setMsg(dragonBoatFestivalEntry.getZong());
            y.setGroupNumber(group);
            jiLuEntryRepository.save(y);

            dragonBoatFestivalRepository.save(dragonBoatFestivalEntry0);
            dragonBoatFestivalRepository.save(dragonBoatFestivalEntry);
            return "一不小心被对方抢了一个\n美味的 "+dragonBoatFestivalEntry0.getZong();
        }
        /**
         * 记录操作
         */
        JiLuEntry y = new JiLuEntry();
        y.setQ0n(0);
        y.setQ1n(0);
        y.setQq0(qq);
        y.setQq1(qq1);
        y.setTime(getTime());
        y.setType(粽子.抢粽子);
        y.setGroupNumber(group);
        jiLuEntryRepository.save(y);
        return "不要气馁哦！\n说不定下次就成功了呢！";
    }


    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat dfHH=new SimpleDateFormat("HH");
    /**
     * 粽子兑奖
     * @param qq
     * @param group
     * @return
     */
    public String rowzz(String qq, String group) throws ParseException {
        List<DragonBoatFestivalEntry> dragonBoatFestivalEntries = dragonBoatFestivalRepository.byQQAndToDay(qq);
        if(dragonBoatFestivalEntries == null||dragonBoatFestivalEntries.size()==0){
            return "暂时没有可兑奖的粽子哦！";
        }
        /**
         * 过滤出中奖的记录
         */
        List<DragonBoatFestivalEntry> dragonBoatFestivalEntryList = dragonBoatFestivalEntries.stream()
                .filter((DragonBoatFestivalEntry b) -> b.getZong().equals(zzRo1) || b.getZong().equals(zzRo2) || b.getZong().equals(zzRo3))
                .collect(Collectors.toList());
        if (dragonBoatFestivalEntryList == null||dragonBoatFestivalEntryList.size()==0) {
            return "没有中奖哦！";
        }

        /**
         * 列出中奖列表
         */
        String msg = "今日的好运粽：\n" +
                "︵︵︵︵︵︵︵︵︵︵\n" +
                "\t*"+zzRo1+"\n" +
                "\t*"+zzRo2+"\n" +
                "\t*"+zzRo3+"\n" +
                "︶︶︶︶︶︶︶︶︶︶\n";
        int numN = 0;
        double hbN = 0;
        String txt = "";

        StringJoiner stringJoiner = new StringJoiner("\n\uD83C\uDF65\uD83C\uDF65\uD83C\uDF65\uD83C\uDF65\uD83C\uDF65\uD83C\uDF65\uD83C\uDF65\uD83C\uDF65\n", "", "");
        for (DragonBoatFestivalEntry dragonBoatFestivalEntry : dragonBoatFestivalEntryList) {
            int yb = 粽子.eatYb()+(int)(10*Math.random())+10;
            txt+="/";
            double hb = 粽子.eatHb();

            if(new Integer(dfHH.format(df.parse(dragonBoatFestivalEntry.getAddDate())))>=20){
                hb = 0;
                yb = 2;
            }

            txt+=dragonBoatFestivalEntry.getZong()+",";
            txt+=yb+",";
            txt+=hb+",";
            txt+="/";

            numN+=yb;
            hbN+=hb;
            stringJoiner.add(dragonBoatFestivalEntry.getZong()+"\n获得悦币："+yb+"\n获得红包："+hb);
        }

        JiLuEntry y = new JiLuEntry();
        y.setN(numN);
        y.setMsg(txt);
        y.setQq0(qq);
        y.setTime(getTime());
        y.setType(粽子.万粽归心);
        y.setGroupNumber(group);
        jiLuEntryRepository.save(y);
        /**
         * 修改状态
         */
        dragonBoatFestivalRepository.updateU(qq);
        UserEntry user = userEntryService.getUser(qq);
        user.payMe(numN);
        user.payMe(hbN);
        userEntryRepository.save(user);

        return msg + stringJoiner;

    }
}

